//
//  SynExericisesPaperData.m
//  MobileStudy
//
//  Created by cuihongbao on 13-6-4.
//  Copyright (c) 2013年 hongbao.cui. All rights reserved.
//

#import "SynExericisesPaperData.h"
#import "GDataXMLNode.h"
@implementation SynExericisesPaperData
-(NSMutableDictionary *)PaperData:(NSString *)xmlString{
    NSError *error=nil;
    GDataXMLDocument *XMLDoc = [[GDataXMLDocument alloc] initWithXMLString:xmlString options:1 error:&error];
    NSMutableDictionary *dataDict = [NSMutableDictionary dictionaryWithCapacity:1];
    if (!error&&XMLDoc) {
        GDataXMLElement *rootElement = [XMLDoc rootElement];
        NSArray *successArray = [rootElement nodesForXPath:@"papersource" error:&error];
        GDataXMLDocument *successElement = [successArray objectAtIndex:0];
        if (!error) {
            NSArray *papertitleArray = [successElement nodesForXPath:@"papertitle" error:&error];
            NSArray *paperTimeArray = [successElement nodesForXPath:@"papertime" error:&error];
            NSArray *paperScoreArray = [successElement nodesForXPath:@"paperscore" error:&error];
            if ([paperTimeArray count]>0&&[papertitleArray count]>0&&[paperScoreArray count]>0&&!error) {
                NSString *paperTitle = [[papertitleArray objectAtIndex:0] stringValue];
                NSString *paperTime = [[paperTimeArray objectAtIndex:0] stringValue];
                NSString *paperScore = [[paperScoreArray objectAtIndex:0] stringValue];
                [dataDict setObject:paperTitle forKey:@"paper_title"];
                [dataDict setObject:paperTime forKey:@"paper_time"];
                [dataDict setObject:paperScore forKey:@"paper_score"];
            }
        }
        NSArray *answerArray = [rootElement nodesForXPath:@"questions" error:&error];
        GDataXMLDocument *answerElement = [answerArray objectAtIndex:0];
        FMSCHBLog(@"answerArray===%d",[answerArray count]);
        NSMutableString *questionStr = [NSMutableString stringWithCapacity:1];
        if (!error) {
             NSArray *answerTypeArray = [answerElement nodesForXPath:@"type" error:&error];
            NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
            NSInteger pageCout = 0;
            for (int i=0; i<[answerTypeArray count]; i++) {
                @autoreleasepool {
                NSMutableDictionary *answerDict = [NSMutableDictionary dictionaryWithCapacity:1];
                GDataXMLDocument *dataXMLDocument = [answerTypeArray objectAtIndex:i];
                GDataXMLNode *xmltype_nameNode = [[answerTypeArray objectAtIndex:i] attributeForName:@"type_name"];
                NSString *type_name = [xmltype_nameNode stringValue];//单选
                [answerDict setObject:type_name forKey:@"type_name"];
                [answerDict setObject:[NSNumber numberWithInteger:i] forKey:@"myqtype"];
                NSArray *questionArray = [dataXMLDocument nodesForXPath:@"question" error:&error];
                NSMutableArray *oneArray = [NSMutableArray arrayWithCapacity:1];
                FMSCHBLog(@"type_name:%@",type_name);
                FMSCHBLog(@"answerTypeArray---------------------------------%d",i);
                for (int j=0; j<[questionArray count]; j++) {
                    NSMutableDictionary *tempDict = [NSMutableDictionary dictionaryWithCapacity:1];
                    GDataXMLDocument *answerElement = [questionArray objectAtIndex:j];
                    NSArray *tqIdArray = [answerElement nodesForXPath:@"tqId" error:&error];
                    if ([tqIdArray count]>0) {
                        GDataXMLNode *tqIdElement = [tqIdArray objectAtIndex:0];
                        NSString *tqId = [tqIdElement stringValue];
                        [tempDict setObject:tqId forKey:@"tq_id"];
                    }
                    NSArray *qtypeArray = [answerElement nodesForXPath:@"qtype" error:&error];
                    if ([qtypeArray count]>0) {
                        GDataXMLNode *qtypeElement = [qtypeArray objectAtIndex:0];
                        NSString *qtype = [qtypeElement stringValue];
                        [tempDict setObject:qtype forKey:@"qtype"];
                    }
                    NSArray *timeArray = [answerElement nodesForXPath:@"time" error:&error];
                    if ([timeArray count]>0) {
                        GDataXMLNode *timeElement = [timeArray objectAtIndex:0];
                        NSString *time= [timeElement stringValue];
                        [tempDict setObject:time forKey:@"time"];
                    }
                    NSArray *scoreArray = [answerElement nodesForXPath:@"score" error:&error];
                    if ([scoreArray count]>0) {
                        GDataXMLNode *scoreElement = [scoreArray objectAtIndex:0];
                        NSString *score= [scoreElement stringValue];
                        [tempDict setObject:score forKey:@"score"];
                    }
                    NSArray *difficultIdArray = [answerElement nodesForXPath:@"difficultId" error:&error];
                    if ([difficultIdArray count]>0) {
                        GDataXMLNode *difficultIdElement = [difficultIdArray objectAtIndex:0];
                        NSString *difficultId= [difficultIdElement stringValue];
                        [tempDict setObject:difficultId forKey:@"difficult_id"];
                    }
                    NSArray *difficultNameArray = [answerElement nodesForXPath:@"difficultId" error:&error];
                    if ([difficultNameArray count]>0) {
                        GDataXMLNode *difficult_nameElement = [difficultNameArray objectAtIndex:0];
                        NSString *difficult_name= [difficult_nameElement stringValue];
                        [tempDict setObject:difficult_name forKey:@"difficult_name"];
                    }
                    NSArray *categoryIdArray = [answerElement nodesForXPath:@"categoryId" error:&error];
                    if ([categoryIdArray count]>0) {
                        GDataXMLNode *categoryIdElement = [categoryIdArray objectAtIndex:0];
                        NSString *categoryId= [categoryIdElement stringValue];
                        [tempDict setObject:categoryId forKey:@"category_id"];
                    }
                    NSArray *categoryNameArray = [answerElement nodesForXPath:@"categoryName" error:&error];
                    if ([categoryNameArray count]>0) {
                        GDataXMLNode *categoryNameElement = [categoryNameArray objectAtIndex:0];
                        NSString *categoryName= [categoryNameElement stringValue];
                        [tempDict setObject:categoryName forKey:@"category_name"];
                    }
                    NSArray *answerArray = [answerElement nodesForXPath:@"answer" error:&error];
                    if ([answerArray count]>0) {
                        GDataXMLNode *reslutElement = [answerArray objectAtIndex:0];
                        NSString *reslut= [reslutElement stringValue];
                        [tempDict setObject:reslut forKey:@"answer"];
                    }
                    NSArray *has_subArray = [answerElement nodesForXPath:@"has_sub" error:&error];
                    if ([has_subArray count]>0) {
                        GDataXMLNode *has_sub_Element = [has_subArray objectAtIndex:0];
                        NSString *has_sub= [has_sub_Element stringValue];
                        [tempDict setObject:has_sub forKey:@"has_sub"];
                    }                   
                    NSArray *analysisArray = [answerElement nodesForXPath:@"analysis" error:&error];
                    if ([analysisArray count]>0) {
                        GDataXMLNode *analysisElement = [analysisArray objectAtIndex:0];
                        NSString *analysis= [analysisElement stringValue];
                        [tempDict setObject:analysis forKey:@"analysis"];
                    }else{
                        [tempDict setObject:@"" forKey:@"analysis"];

                    }
                    NSArray *orderidArray = [answerElement nodesForXPath:@"orderid" error:&error];
                    if ([orderidArray count]>0) {
                        GDataXMLNode *orderidElement = [orderidArray objectAtIndex:0];
                        NSString *orderid= [orderidElement stringValue];
                        [tempDict setObject:orderid forKey:@"orderid"];
                    }else{
                        NSString *orderid = [NSString stringWithFormat:@"%d",j];
                        [tempDict setObject:orderid forKey:@"orderid"];
                    }
                    NSArray *qstIdArray = [answerElement nodesForXPath:@"qstId" error:&error];
                    if ([qstIdArray count]>0) {
                        GDataXMLNode *orderidElement = [qstIdArray objectAtIndex:0];
                        NSString *qstId= [orderidElement stringValue];
                        [tempDict setObject:qstId forKey:@"qstId"];
                    }else{
                        NSString *qstId = [NSString stringWithFormat:@"%ld",pageCout];
                        [tempDict setObject:qstId forKey:@"qstId"];
                        pageCout++;
                    }
                    NSArray *parentidArray = [answerElement nodesForXPath:@"parentid" error:&error];
                    if ([parentidArray count]>0) {
                        GDataXMLNode *parentidElement = [[answerElement nodesForXPath:@"parentid" error:&error] objectAtIndex:0];
                        NSString *parentid= [parentidElement stringValue];
                        [tempDict setObject:parentid forKey:@"parent_id"];
                    }
                    NSArray *questionContentArray = [answerElement nodesForXPath:@"questionContent" error:&error];
                    if ([questionContentArray count]>0) {
                        GDataXMLNode *questionContentElement = [questionContentArray objectAtIndex:0];
                        NSString *questionContent= [questionContentElement stringValue];
                        FMSCHBLog(@"questionContent:%@",questionContent);
                        FMSCHBLog(@"---j------------%d",j);
                        [tempDict setObject:questionContent forKey:@"questionContent"];
                    }
                    NSArray *optionsArray = [answerElement nodesForXPath:@"options" error:&error];
                    if ([optionsArray count]>0) {
                        GDataXMLNode *optionsContentElement = [optionsArray objectAtIndex:0];
                        //                    NSString *questionsContent= [optionsContentElement stringValue];
                        NSArray *optionArray =[optionsContentElement nodesForXPath:@"option" error:&error];
                        NSMutableArray *option = [NSMutableArray arrayWithCapacity:1];
                        for (int n=0; n<[optionArray count]; n++) {
                            GDataXMLElement *optionElement = [optionArray objectAtIndex:n];
                            GDataXMLNode *xmloptionIndexNode = [optionElement attributeForName:@"optionIndex"];
                            NSString *indexStr = [NSString stringWithFormat:@"%@.%@",[xmloptionIndexNode stringValue],[optionElement stringValue]];
                            [option addObject:indexStr];
                        }
                        [tempDict setObject:option forKey:@"options"];
                        
                        [oneArray addObject:tempDict];
                    }
                }
                
                [answerDict setObject:oneArray forKey:@"question"];
                [tempArray addObject:answerDict];
            }
            }
            [dataDict setObject:tempArray forKey:@"questions"];
        }else{
            return nil;
        }
        return dataDict;
    }else{
        return nil;
    }
}
-(NSString *)PaperTitleData:(NSString *)xmlString keyName:(NSString *)keyName{
    NSError *error=nil;
    GDataXMLDocument *XMLDoc = [[GDataXMLDocument alloc] initWithXMLString:xmlString options:1 error:&error];
    NSMutableDictionary *dataDict = [NSMutableDictionary dictionaryWithCapacity:1];
    if (!error&&XMLDoc) {
        GDataXMLElement *rootElement = [XMLDoc rootElement];
        NSArray *successArray = [rootElement nodesForXPath:@"papersource" error:&error];
        GDataXMLDocument *successElement = [successArray objectAtIndex:0];
        NSString *paperTitle=nil;
        if (!error) {
            NSArray *papertitleArray = [successElement nodesForXPath:keyName error:&error];
            if ([papertitleArray count]>0&&!error) {
                paperTitle = [[papertitleArray objectAtIndex:0] stringValue];
                [dataDict setObject:paperTitle forKey:paperTitle];
            }
        }
        return paperTitle;
    }else{
        return nil;
    }
}
@end
